import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.junit.Test;

import java.util.Collections;

public class GenCode {

    @Test
    public void run () {
        String currentPath = GenCode.class.getClassLoader().getResource(".").getPath();
        String path = currentPath.substring(0, currentPath.indexOf("/target/test-classes/"));
        FastAutoGenerator.create("jdbc:mysql://localhost.site:3306/guli?useSSL=false", "root", "123456")
                .globalConfig(builder -> {
                    builder.author("YongTay") // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .outputDir(path + "/src/main/java"); // 指定输出目录
                })
                // /home/yongtay/Code/java/guli/guli-parent/service/service-edu/src/main/java
                .packageConfig(builder -> {
                    builder.parent("cn.aylog") // 设置父包名
                            .moduleName("springboot") // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, path + "/src/main/resources/mappers")); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.addInclude("edu_course") // 设置需要生成的表名
                            .addTablePrefix("edu_"); // 设置过滤表前缀
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }

    public static void main(String[] args) {
        String currentPath = GenCode.class.getClassLoader().getResource(".").getPath();
        String path = currentPath.substring(0, currentPath.indexOf("/target/test-classes/"));
        System.out.println(path);
    }
}
